package com.woniuxy.ticketcinema.serviec.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticketcinema.entity.TicketSeat;
import com.woniuxy.ticketcinema.mapper.SeatMapper;
import com.woniuxy.ticketcinema.serviec.SeatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName SeatServiceImpl
 * @Description TODO
 * @Author zx
 * @Date 2021-02-27 15:06:30
 * @Version 1.0
 */
@Service
@Slf4j
public class SeatServiceImpl extends ServiceImpl<SeatMapper, TicketSeat> implements SeatService {

    @Resource
    private SeatMapper seatMapper;

    @Override
    public String findSeatIdByCoord(String seat) {
        //int rowno, int colno
        String[] location = seat.split("\\,");
        //获取座位id(可能获取失败？座位是否有效？？？) 切割之后String转int
        QueryWrapper queryWrapper = new QueryWrapper<TicketSeat>();
        queryWrapper.eq("rowno", Integer.parseInt(location[0]));
        //判断
        if (seatMapper.selectCount(queryWrapper)==0){
            return "请填写正确的排";
        }
        queryWrapper.eq("colno", Integer.parseInt(location[1]));
        if (seatMapper.selectCount(queryWrapper)==0){
            return "请填写正确的号";
        }
        int seatId = seatMapper.findSeatIdByCoord(Integer.parseInt(location[0]), Integer.parseInt(location[1]));
        //查询座位是否可用  >0可以坐
        if (seatMapper.findStatusBySeat(Integer.parseInt(location[0]), Integer.parseInt(location[1]))>0){
            //查看座位是否可以售卖(是否已经被购买)
            if (seatMapper.findStatusBySeatId(seatId).equals("1")){
                return "该座位已经被购买!";
            }
            //是否购买成功    >0购买成功
            if(seatMapper.updateStatus(seatId)>0){
                //购票成功
                return "购票成功";
            }else {
                //购票失败
                return "购票失败";
            }
        }else {
            //座位坏了
            return "座位暂时不能购买";
        }
    }
}
